<template>
  <div class="ToolingType">
    <el-row style="paddingBottom: 0">
      <el-col :span="24">
        <dynForm :info-form="searchForm" :search-tags="searchTags" :select-list="selectList">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}</el-button
            >
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <!-- v-btnPermission:add -->
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="newAdd"
      >{{ $t("add") }}</el-button
      >
      <!-- v-btnPermission:edit -->
      <el-button
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="doModify"
      >{{ $t("modify") }}</el-button
      >
      <!-- v-btnPermission:delete -->
      <el-button
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="doDelete"
      >{{ $t("delete") }}</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-download"
        @click="handleExport"
      >{{ $t("export") }}</el-button
      >
    </div>
    <!-- 表单部分 -->
    <el-card shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="onPagination"
      />
    </el-card>
    <AppBindModel ref="AppBindModel" @initList="initList"/>
  </div>
</template>

<script>
import mySetting from '@/mixins/mySetting'
import { getList, doDelete } from '@/api/equipment/appBind'
import AppBindModel from './components/appBindModel.vue'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

export default {
  name: 'AppBind',
  components: { AppBindModel },
  mixins: [mySetting],
  data() {
    return {
      searchForm: {},
      searchTags: [
        { label: this.$t('eqpId'), prop: 'eqpId', tag: 'DynInput' },
        { label: this.$t('appMac'), prop: 'appmac', tag: 'DynInput' }
      ],
      selectList: {},
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        { prop: 'eqpId', label: this.$t('eqpId') },
        { prop: 'appMac', label: this.$t('appMac') },
        { prop: 'projectCode', label: this.$t('projectCode') },
        { prop: 'remark', label: this.$t('remark') },
        { prop: 'creator', label: this.$t('creator') },
        { prop: 'createTime', label: this.$t('createTime') }
      ],
      tableLoading: true,
      headers: {
        Token: getToken()
      }
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  created() {
    this.initList()
  },
  methods: {
    async initList() {
      const res = await getList(this.$clearNull({ ...this.searchForm, ...this.listQuery }))
      this.tableData = res.data
      this.total = res.total
      this.tableLoading = false
    },
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    newAdd() {
      this.$refs.AppBindModel.handleOpen()
    },
    doModify() {
      this.$refs.AppBindModel.handleOpen(this.multipleSelection[0])
    },
    doDelete() {
      this.$confirm(
        `${this.$t('confirmDelete')}${this.multipleSelection.length}${this.$t(
          'recordCount'
        )}?`,
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map(ele => ele.id)
        doDelete({ ids }).then(res => {
          this.$betterTips(res, this.setOnePage, this.initList)
        })
      })
    },
    onPagination(page) {
      this.listQuery = page
      this.initList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    uploadSuccess(response, file, fileList) {
      if (response.code !== 1) {
        const h = this.$createElement
        let listVNode
        if (response.data instanceof Array) {
          listVNode = response.data.map((item) => {
            return (
              <p style={ { width: '240px' }}>
                <span style={ { width: '100%', wordWrap: 'break-word' }}>{item.index ? `第{${item.index}}行` : ''}{this.$t(item.msgCode)}</span>
              </p>
            )
          })
          const styles = {
            color: 'teal',
            overflowX: 'hidden',
            overflowY: 'auto',
            width: '280px',
            maxHeight: 'calc(100vh - 120px)'
          }
          this.$notify({
            message: h('div', { style: styles }, listVNode),
            type: 'error',
            duration: 0
          })
        } else {
          this.$betterTips(response)
        }

        fileList = []
      } else {
        this.$betterTips(response, this.initList)
      }
    },
    importFail() {
      this.$notify({
        message: this.$t('uploadFail'),
        type: 'error',
        duration: 3500
      })
    },
    handleExport() {
      getList(this.$clearNull({ ...this.searchForm, ...this.listQuery, toexcel: 1 })).then(res => {
        if (res.code === 1) {
          window.open(this.baseApi + '/' + res.data)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
.upload-btn{
  margin: 0 10px;
}
</style>
